package com.lin.server.task;

import com.lin.entity.PlaneData;
import com.lin.mapper.PlaneDataMapper;
import com.lin.server.ws.WsDataHolder;
import com.lin.utils.CollectionUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 飞机目标数据入库任务
 */
@Slf4j
public class PlaneDataInTask implements Runnable {

    private final WsDataHolder wsDataHolder;

    private final PlaneDataMapper planeDataMapper;

    public PlaneDataInTask(WsDataHolder wsDataHolder, PlaneDataMapper planeDataMapper) {
        this.wsDataHolder = wsDataHolder;
        this.planeDataMapper = planeDataMapper;
    }

    @Override
    public void run() {

        try {
            // 取出数据
            List<PlaneData> planeDataList = wsDataHolder.getPlaneDataList();

            if (CollectionUtils.isNotEmpty(planeDataList) && planeDataList.size() >= WsDataHolder.BATCH_COUNT) {
                planeDataMapper.batchInsert(planeDataList);
                wsDataHolder.setPlaneDataList(new ArrayList<>(WsDataHolder.BATCH_COUNT));
            }
        } catch (Exception e) {
            log.error("飞机目标数据入库任务执行出错：", e);
        }
    }

    /**
     * 清空缓存的飞机数据
     */
    public void clearPlaneData(){
        List<PlaneData> planeDataList = wsDataHolder.getPlaneDataList();
        if (CollectionUtils.isNotEmpty(planeDataList)) {
            planeDataMapper.batchInsert(planeDataList);
            wsDataHolder.setPlaneDataList(new ArrayList<>(WsDataHolder.BATCH_COUNT));
        }
    }
}
